// set and cache variables
		var w, container, carousel, item, radius, itemLength, rY, ticker, fps; 
		var mouseX = 0;
		var mouseY = 0;
		var mouseZ = 0;
		var addX = 0;
		var configdata;
		var isPress=0;
		
		// fps counter created by: https://gist.github.com/sharkbrainguy/1156092,
		// no need to create my own :)
		var fps_counter = {
			
			tick: function () 
			{
				// this has to clone the array every tick so that
				// separate instances won't share state 
				this.times = this.times.concat(+new Date());
				var seconds, times = this.times;
        
				if (times.length > this.span + 1) 
				{
					times.shift(); // ditch the oldest time
					seconds = (times[times.length - 1] - times[0]) / 1000;
					return Math.round(this.span / seconds);
				} 
				else return null;
			},
 
			times: [],
			span: 20
		};
		var counter = Object.create(fps_counter);
		
		
		
		$(document).ready(function(){
			initDate();
			init();
		})
		
		function initDate(){
			initPlayer();
		}
		function init()
		{
			
			getPlayer();
			w = $(window);
			container = $( '#contentContainer' );
			carousel = $( '#carouselContainer' );

			item = $( '.carouselItem' );
			itemLength = $( '.carouselItem' ).length;
			fps = $('#fps');
			rY = 360 / itemLength;
			radius = Math.round( (250) / Math.tan( Math.PI / itemLength ) );
			
			// set container 3d props
			TweenMax.set(container, {perspective:600})
			TweenMax.set(carousel, {z:-(radius)})
			
			// create carousel item props
			
			for ( var i = 0; i < itemLength; i++ )
			{
				var $item = item.eq(i);
				var $block = $item.find('.carouselItemInner');
				
        //thanks @chrisgannon!        
        TweenMax.set($item, {rotationY:rY * i, z:radius, transformOrigin:"50% 50% " + -radius + "px"});
				
				animateIn( $item, $block )						
			}
			
			// set mouse x and y props and looper ticker
			window.addEventListener( "mousemove", onMouseMove, false );
			ticker = setInterval( looper, 1000/60 );	

 			document.onkeydown = function (e) {
            e = e || event;
            	if (e.keyCode == 32) {  //判断是否单击的space键盘
				
					window.removeEventListener("mousemove",onMouseMove,false);
	             	 mouseX = 0;
					//计算所有图片真实面积，取面积最大者为获奖者
					/*for(var i=0;i<configdata.length;i++){
						if(configdata[i].isplay==1&&configdata[i].staus==0){
							var imgheight = $("#"+configdata[i].index).height();
							var imgwidth = $("#"+configdata[i].index).width();
							console.log("imgheight:"+imgheight+",imgwidth:"+imgwidth);
						}
					}
					*/
					if(isPress == 0){
						var num =0;
						for(var i=0;i<configdata.length;i++){
							if(configdata[i].isplay=="1"&&configdata[i].staus=="0"){
								num++;
							}
						}
						
						var ran = parseInt(Math.random()*(num))+1; 
						console.log(ran+"/"+num);
						var imgIndex = 0;
						for(var i=0;i<configdata.length;i++){
							if(ran == 0){
								break;
							}
							if(configdata[i].isplay=="1"&&configdata[i].staus=="0"){
								imgIndex++;
								ran--;

							}else{
								imgIndex++;
							}
						}
						configdata[imgIndex-1].staus = 1;
						console.log(imgIndex+":"+configdata[imgIndex-1].img);
						$('#selectItem').append("<img src=images1/"+configdata[imgIndex-1].img+" width='100%' height='100%'></img>");
						
						container.hide();
						isPress = 1;
					}
					
	 			 	return true;
            	}
   			}
			
			document.onmousedown = function(e){
				isPress = 0;
				 $( '#carouselContainer' ).empty();
				init();

				e=e || event;
				//如果点击鼠标左键增加事件onmousemove事件
				container.show();
				$('#selectItem').empty();
				if(e.button==0){
					window.addEventListener( "mousemove", onMouseMove, false );
				}
			}

		}
		
		
		function animateIn( $item, $block )
		{
			var $nrX = 360 * getRandomInt(2);
			var $nrY = 360 * getRandomInt(2);
				
			var $nx = -(2000) + getRandomInt( 4000 )
			var $ny = -(2000) + getRandomInt( 4000 )
			var $nz = -4000 +  getRandomInt( 4000 )
				
			var $s = 1.5 + (getRandomInt( 10 ) * .1)
			var $d = 1 - (getRandomInt( 8 ) * .1)
			
			TweenMax.set( $item, { autoAlpha:1, delay:$d } )	
			TweenMax.set( $block, { z:$nz, rotationY:$nrY, rotationX:$nrX, x:$nx, y:$ny, autoAlpha:0} )
			TweenMax.to( $block, $s, { delay:$d, rotationY:0, rotationX:0, z:0,  ease:Expo.easeInOut} )
			TweenMax.to( $block, $s-.5, { delay:$d, x:0, y:0, autoAlpha:1, ease:Expo.easeInOut} )
		}
		
		function onMouseMove(event)
		{
			mouseX = -(-(window.innerWidth * .5) + event.pageX) * .005;
			mouseY = -(-(window.innerHeight * .5) + event.pageY ) * .01;
			mouseZ = -(radius) - (Math.abs(-(window.innerHeight * .5) + event.pageY ) - 200);
		}

		
		// loops and sets the carousel 3d properties
		function looper()
		{
			addX += mouseX
			TweenMax.to( carousel, 1, { rotationY:addX, rotationX:mouseY, ease:Quint.easeOut } )
			TweenMax.set( carousel, {z:mouseZ } )
			fps.text( 'Framerate: ' + counter.tick() + '/60 FPS' )
			
		}
		
		function getRandomInt( $n )
		{
			return Math.floor((Math.random()*$n)+1);	
		}


		function initPlayer(){
			
		configdata=[
						{
							"index":"item01",
								"name":"李涛",
								"img":"李涛.jpg",
								"isplay":"1",
								"staus":"0"
						},

						{
							"index":"item02",
								"name":"舒翀",
								"img":"舒翀.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item04",
								"name":"吕若慈",
								"img":"吕若慈.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item05",
								"name":"李蓓华",
								"img":"李蓓华.JPG",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item06",
								"name":"石泽兵",
								"img":"石泽兵.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item07",
								"name":"葛行",
								"img":"葛行.JPG",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item08",
								"name":"赵媛",
								"img":"赵媛.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item09",
								"name":"金磊",
								"img":"金磊.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item10",
								"name":"方琼兰",
								"img":"方琼兰.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item11",
								"name":"闫沁",
								"img":"闫沁.JPG",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item12",
								"name":"孟晓霞",
								"img":"孟晓霞.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item13",
								"name":"沈越宁",
								"img":"沈越宁.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item14",
								"name":"曾珮",
								"img":"曽珮.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item15",
								"name":"胡峰",
								"img":"胡峰.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item16",
								"name":"顾赛波",
								"img":"顾赛波.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item17",
								"name":"苏佳龙",
								"img":"苏佳龙.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item18",
								"name":"吴涛",
								"img":"吴涛.jpg",
								"isplay":"0",
								"staus":"0"
						},
						{
							"index":"item19",
								"name":"范玉婷",
								"img":"范玉婷.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item20",
								"name":"王云路",
								"img":"王云路.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item21",
								"name":"陈悦悦",
								"img":"陈悦悦.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item22",
								"name":"黄嘉丽",
								"img":"黄嘉丽.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item23",
								"name":"宋富强",
								"img":"宋富强.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item24",
								"name":"梁渝",
								"img":"梁渝.jpg",
								"isplay":"0",
								"staus":"0"
						},
						{
							"index":"item25",
								"name":"丁亚乐",
								"img":"丁亚乐.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item26",
								"name":"郑志毅",
								"img":"郑志毅.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item27",
								"name":"王其明",
								"img":"王其明.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item28",
								"name":"邓梁",
								"img":"邓梁.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item29",
								"name":"杨玲",
								"img":"杨玲.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item30",
								"name":"景文恩",
								"img":"景文恩.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item31",
								"name":"王媛媛",
								"img":"王媛媛.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item32",
								"name":"钱春燕",
								"img":"钱春燕.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item33",
								"name":"孙一兵",
								"img":"孙一兵.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item34",
								"name":"陈婷婷",
								"img":"陈婷婷.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item35",
								"name":"张佳玲",
								"img":"张佳玲.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item36",
								"name":"蒋静",
								"img":"蒋静.jpg",
								"isplay":"1",
								"staus":"0"
						},
						{
							"index":"item37",
								"name":"杨春梅",
								"img":"杨春梅.jpg",
								"isplay":"1",
								"staus":"0"
						},

						{
							"index":"item39",
								"name":"徐鹏",
								"img":"徐鹏.jpg",
								"isplay":"1",
								"staus":"0"
						}
					]

		}

		function getPlayer(){
		/*
		$.getJSON("data/config.json",function(data){
			var json = eval(data);
			console.log(json);
		})
		*/
		/*
		$.ajax({
				url:"data/config.json",
				dataType: "json",
				success:function(data){
					console.log("加载成功");
				},
				error:function(){
					console.log("加载失败！");
				}
			})
		*/
		//chrome不支持本地json数据加载，以上方法不能用
		//遍历configdata，往carouselContainer中添加展示container或者图片
		

			for(var i=0;i<configdata.length;i++){
				if(configdata[i].isplay=="1"&&configdata[i].staus=="0"){
					 $( '#carouselContainer' ).append("<figure  class='carouselItem trans3d'><div><img id="+configdata[i].index+" ondragstart=dragStart(event) src=images/"+configdata[i].img+" draggable=true width='400' height='247.2'></img></div></figure>")
				}
				
			}

		}

		function show(){
			
		}

        function allowDrop(e) {
            e.preventDefault();
        }
        function dragStart(event) {
            var dataTransfer = event.dataTransfer;
            dataTransfer.setData("id", event.target.id);
        }

        function drop(event) {
            var dataTransfer = event.dataTransfer;
            var imgId = dataTransfer.getData("id");
            var img = document.getElementById(imgId);
			for(var i=0;i<configdata.length;i++){
				if(configdata[i].index == imgId){
					configdata[i].staus = 1;
				}
			}
            var div = event.target;
            div.appendChild(img);
			 $( '#carouselContainer' ).empty();
			 $('#selectItem').empty();
			 init();

        }
